package com.taxi.driver.module.order.pool;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.taxi.driver.R;
import com.taxi.driver.common.BaseFragment;
import com.taxi.driver.event.OrderEvent;
import com.taxi.driver.module.order.popup.OrderPopupActivity;
import com.taxi.driver.module.vo.OrderSummaryVO;
import com.taxi.driver.socket.SocketPushContent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by FTL
 */
public class PoolFragment extends BaseFragment implements PoolContract.View {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.ll_empty)
    LinearLayout mLlEmpty;

    @Inject
    PoolPresenter mPresenter;

    private PoolAdapter mAdapter;

    public static PoolFragment newInstance() {
        Bundle args = new Bundle();
        PoolFragment fragment = new PoolFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_pool, container, false);
        ButterKnife.bind(this, mView);

        initView();

        return mView;
    }

    private void initView() {
        mAdapter = new PoolAdapter(getContext());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener((position, view, item) -> OrderPopupActivity.start(getActivity(), item.uuid, false));
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        DaggerPoolComponent.builder()
                .appComponent(getAppComponent())
                .poolModule(new PoolModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.subscribe();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        mPresenter.unsubscribe();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void setList(List<OrderSummaryVO> vos) {
        if (vos.size() == 0) {
            mLlEmpty.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        } else {
            mLlEmpty.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.VISIBLE);
            mAdapter.setAll(vos);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(OrderEvent event) {
        switch (event.type) {
            case OrderEvent.ORDER_PUSH:
            case OrderEvent.ORDER_HAS_BEEN_ROBBED:
            case OrderEvent.ORDER_MATCH_THE_TIMEOVER:
            case OrderEvent.ORDER_CLOSE:
                mPresenter.getList();
                break;
            case OrderEvent.ORDER_PASSENGER_CANCEL:
                SocketPushContent content = (SocketPushContent) event.obj1;
                if (content == null) return;
                if (content.data.hasDriver != null && !content.data.hasDriver) {
                    mPresenter.getList();
                }
                break;
        }
    }
}
